#include "main_window.h"

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>

#include "support.h"
#include "preferences.h"

const char* authors[] = {"Peter Pawlowski","Walter Wang"};
const char* copyright = "Copyright (c) 2001-2009, Peter Pawlowski\n(c) 2009 Walter Wang";
const char* license = "\
\
Redistribution and use in source and binary forms, with or without\n\
modification, are permitted provided that the following conditions are met:\n\
\n\
Redistributions of source code must retain the above copyright notice, this\n\
list of conditions and the following disclaimer.  \n\
Neither the name of the author nor the names of its contributors may be used to\n\
endorse or promote products derived  from this software without specific prior\n\
written permission.\n\
Redistributions in  binary form must reproduce the above copyright notice, this\n\
list of conditions and  the following disclaimer in the documentation or other\n\
materials provided with the  distribution.  \n\
\n\
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n\
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n\
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n\
DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY\n\
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n\
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n\
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n\
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n\
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n\
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";


MainWindow::MainWindow()
{

// menubar
    GtkWidget *mb_item_file;
    GtkWidget *mb_item_edit;
    GtkWidget *mb_item_view;
    GtkWidget *mb_item_playback;
    GtkWidget *mb_item_library;
    GtkWidget *mb_item_help;

    GtkWidget *menu_file;
    GtkWidget *menu_edit;
    GtkWidget *menu_view;
    GtkWidget *menu_playback;
    GtkWidget *menu_library;
    GtkWidget *menu_help;

    GtkWidget *menu_item_open;
    GtkWidget *menu_item_open_dir;
    GtkWidget *menu_item_separator;
    GtkWidget *menu_item_pref;
    GtkWidget *menu_item_quit;

    GtkWidget *menu_item_about;

    accel_group = gtk_accel_group_new ();

    _main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(_main_window), 640, 480);


    _vbox = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (_vbox);
    gtk_container_add (GTK_CONTAINER (_main_window), _vbox);

    _menubar = gtk_menu_bar_new ();
    gtk_widget_show (_menubar);
    gtk_box_pack_start (GTK_BOX (_vbox), _menubar, FALSE, TRUE, 0);

    ///////////////////////////////////////////////////////////
    // File menu
    //////////////////////////////////////////////////////////
    mb_item_file = gtk_menu_item_new_with_mnemonic (_("_File"));
    gtk_widget_show (mb_item_file);
    gtk_container_add (GTK_CONTAINER (_menubar), mb_item_file);
    menu_file = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mb_item_file), menu_file);

    menu_item_open = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
    gtk_widget_show (menu_item_open);
    gtk_container_add (GTK_CONTAINER (menu_file), menu_item_open);

    menu_item_open_dir = gtk_menu_item_new_with_label("Open directory ...");
    gtk_widget_show (menu_item_open_dir);
    gtk_container_add (GTK_CONTAINER (menu_file), menu_item_open_dir);

    _separator = gtk_separator_menu_item_new ();
    gtk_widget_show (_separator);
    gtk_container_add (GTK_CONTAINER (menu_file), _separator);
    gtk_widget_set_sensitive (_separator, FALSE);

    menu_item_pref = gtk_image_menu_item_new_from_stock ("gtk-preferences", accel_group);
    gtk_widget_show (menu_item_pref);
    gtk_container_add (GTK_CONTAINER (menu_file), menu_item_pref);

    _separator = gtk_separator_menu_item_new ();
    gtk_widget_show (_separator);
    gtk_container_add (GTK_CONTAINER (menu_file), _separator);
    gtk_widget_set_sensitive (_separator, FALSE);

    menu_item_quit = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
    gtk_widget_show (menu_item_quit);
    gtk_container_add (GTK_CONTAINER (menu_file), menu_item_quit);

    ///////////////////////////////////////////////////////////
    // Edit menu
    //////////////////////////////////////////////////////////
    mb_item_edit = gtk_menu_item_new_with_mnemonic (_("_Edit"));
    gtk_widget_show (mb_item_edit);
    gtk_container_add (GTK_CONTAINER (_menubar), mb_item_edit);
    menu_edit = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mb_item_edit), menu_edit);

    imagemenuitem6 = gtk_image_menu_item_new_from_stock ("gtk-cut", accel_group);
    gtk_widget_show (imagemenuitem6);
    gtk_container_add (GTK_CONTAINER (menu_edit), imagemenuitem6);

    imagemenuitem7 = gtk_image_menu_item_new_from_stock ("gtk-copy", accel_group);
    gtk_widget_show (imagemenuitem7);
    gtk_container_add (GTK_CONTAINER (menu_edit), imagemenuitem7);

    imagemenuitem8 = gtk_image_menu_item_new_from_stock ("gtk-paste", accel_group);
    gtk_widget_show (imagemenuitem8);
    gtk_container_add (GTK_CONTAINER (menu_edit), imagemenuitem8);

    imagemenuitem9 = gtk_image_menu_item_new_from_stock ("gtk-delete", accel_group);
    gtk_widget_show (imagemenuitem9);
    gtk_container_add (GTK_CONTAINER (menu_edit), imagemenuitem9);

    ///////////////////////////////////////////////////////////
    // View menu
    //////////////////////////////////////////////////////////
    mb_item_view = gtk_menu_item_new_with_mnemonic (_("_View"));
    gtk_widget_show (mb_item_view);
    gtk_container_add (GTK_CONTAINER (_menubar), mb_item_view);
    menu_view = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mb_item_view), menu_view);

    ///////////////////////////////////////////////////////////
    // Playback menu
    //////////////////////////////////////////////////////////
    mb_item_playback = gtk_menu_item_new_with_mnemonic (_("_Playback"));
    gtk_widget_show (mb_item_playback);
    gtk_container_add (GTK_CONTAINER (_menubar), mb_item_playback);
    menu_playback = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mb_item_playback), menu_playback);

    ///////////////////////////////////////////////////////////
    // Library menu
    //////////////////////////////////////////////////////////
    mb_item_library = gtk_menu_item_new_with_mnemonic (_("_Library"));
    gtk_widget_show (mb_item_library);
    gtk_container_add (GTK_CONTAINER (_menubar), mb_item_library);
    menu_library = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mb_item_library), menu_library);

    ///////////////////////////////////////////////////////////
    // Help menu
    //////////////////////////////////////////////////////////
    mb_item_help = gtk_menu_item_new_with_mnemonic (_("_Help"));
    gtk_widget_show (mb_item_help);
    gtk_container_add (GTK_CONTAINER (_menubar), mb_item_help);
    menu_help = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (mb_item_help), menu_help);

    menu_item_about= gtk_image_menu_item_new_from_stock ("gtk-about", accel_group);
    gtk_widget_show (menu_item_about);
    gtk_container_add (GTK_CONTAINER (menu_help), menu_item_about);

    //Toolbar
    toolbar1 = gtk_toolbar_new ();
    gtk_widget_show (toolbar1);
    gtk_box_pack_start (GTK_BOX (_vbox), toolbar1, FALSE, TRUE, 1);
    gtk_toolbar_set_style (GTK_TOOLBAR (toolbar1), GTK_TOOLBAR_BOTH_HORIZ);
    tmp_toolbar_icon_size = gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar1));

    tmp_image = create_pixmap (_main_window, "prev.png");
    gtk_widget_show (tmp_image);
    toolbutton4 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton4);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton4);

    tmp_image = create_pixmap (_main_window, "pause.png");
    gtk_widget_show (tmp_image);
    toolbutton3 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton3);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton3);

    tmp_image = create_pixmap (_main_window, "play.png");
    gtk_widget_show (tmp_image);
    toolbutton1 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton1);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton1);

    tmp_image = create_pixmap (_main_window, "stop.png");
    gtk_widget_show (tmp_image);
    toolbutton6 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton6);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton6);

    tmp_image = create_pixmap (_main_window, "next.png");
    gtk_widget_show (tmp_image);
    toolbutton5 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton5);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton5);

    tmp_image = create_pixmap (_main_window, "random.png");
    gtk_widget_show (tmp_image);
    toolbutton7 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton7);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton7);

    toolbutton11 = (GtkWidget*) gtk_separator_tool_item_new ();
    gtk_widget_show (toolbutton11);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton11);

    tmp_image = create_pixmap (_main_window, "open.png");
    gtk_widget_show (tmp_image);
    toolbutton2 = (GtkWidget*) gtk_tool_button_new (tmp_image, "");
    gtk_widget_show (toolbutton2);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton2);

    toolbutton8 = (GtkWidget*) gtk_separator_tool_item_new ();
    gtk_widget_show (toolbutton8);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton8);

    toolbutton9 = (GtkWidget*) gtk_tool_item_new ();
    gtk_widget_show (toolbutton9);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton9);

    label1 = gtk_label_new (_("Order: "));
    gtk_widget_show (label1);
    gtk_container_add (GTK_CONTAINER (toolbutton9), label1);

    toolbutton10 = (GtkWidget*) gtk_tool_item_new ();
    gtk_widget_show (toolbutton10);
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton10);

    combobox1 = gtk_combo_box_new_text ();
    gtk_widget_show (combobox1);
    gtk_container_add (GTK_CONTAINER (toolbutton10), combobox1);
    gtk_combo_box_append_text (GTK_COMBO_BOX (combobox1), _("Default"));
    gtk_combo_box_append_text (GTK_COMBO_BOX (combobox1), _("Random"));
    gtk_combo_box_append_text (GTK_COMBO_BOX (combobox1), _("Repeat"));
    gtk_combo_box_append_text (GTK_COMBO_BOX (combobox1), _("Repeat Current"));
    gtk_combo_box_set_add_tearoffs (GTK_COMBO_BOX (combobox1), TRUE);

    _notebook = gtk_notebook_new ();
    gtk_widget_show (_notebook);
    gtk_box_pack_start (GTK_BOX (_vbox), _notebook, TRUE, TRUE, 0);

    treeview1 = gtk_tree_view_new ();
    gtk_widget_show (treeview1);
    gtk_container_add (GTK_CONTAINER (_notebook), treeview1);

    label6 = gtk_label_new (_("default"));
    gtk_widget_show (label6);
    gtk_notebook_set_tab_label (GTK_NOTEBOOK (_notebook), gtk_notebook_get_nth_page (GTK_NOTEBOOK (_notebook), 0), label6);

    _statusbar = gtk_statusbar_new ();
    gtk_widget_show (_statusbar);
    gtk_box_pack_start (GTK_BOX (_vbox), _statusbar, FALSE, TRUE, 0);


    gtk_widget_grab_default (combobox1);
    gtk_window_add_accel_group (GTK_WINDOW (_main_window), accel_group);

    // signal
    g_signal_connect (_main_window, "delete_event", G_CALLBACK (delete_event), NULL);
    g_signal_connect (G_OBJECT(menu_item_quit), "activate", G_CALLBACK (delete_event), NULL);
    g_signal_connect (G_OBJECT(menu_item_pref), "activate", G_CALLBACK (on_pref), NULL);
    g_signal_connect (G_OBJECT(menu_item_about), "activate", G_CALLBACK (on_about), NULL);

    gtk_widget_show_all (_main_window);
}

MainWindow::~MainWindow()
{
    //dtor
}


gboolean MainWindow::delete_event(GtkWidget* widget, GdkEvent *event, gpointer data)
{
    gtk_main_quit();
    return FALSE;
}

/** @brief on_pref
  *
  * @todo: document this function
  */
void MainWindow::on_pref(GtkWidget* widget, gpointer data)
{
    Preferences pref;
}

/** @brief on_about
  *
  * @todo: document this function
  */
void MainWindow::on_about(GtkWidget* widget, gpointer data)
{


    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("gfoobar.png", NULL);

    GtkWidget *dialog = gtk_about_dialog_new();
    gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(dialog), "gfoobar2000");
    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "0.1");
    gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(dialog), authors);
    gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog), copyright);
    gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(dialog), license);
    gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog),
                                  "foobar2000 for linux.");
    gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog),
                                 "http://code.google.com/p/gfoobar2000/");
    gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), pixbuf);
    g_object_unref(pixbuf), pixbuf = NULL;
    gtk_dialog_run(GTK_DIALOG (dialog));
    gtk_widget_destroy(dialog);

}
